package ua.maxicash.service;

import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.ui.velocity.VelocityEngineUtils;
import ua.maxicash.dao.AppDao;
import ua.maxicash.dao.WorkDao;
import ua.maxicash.exceptions.NoSuchEmailInSystemException;
import ua.maxicash.vo.SystemUser;
import ua.maxicash.vo.Work;

import java.util.HashMap;
import java.util.Set;

@Service("NotifyService")
public class NotifyServiceImpl implements NotifyService {

    @Autowired
    WorkDao workDao;

    @Autowired
    AppDao appDao;

    @Autowired
    MessageService messageService;

    @Autowired
    VelocityEngine velocityEngine;

    @Autowired
    MailService mailService;

    @Override
    @Async
    public void notifyLikersAboutNewWork(final Work work) {

        String employersEmail = work.getEmployer().getEmail();
        Set<SystemUser> likers = appDao.getLikesOfSystemUser(work.getEmployer().getIdUser(), 0);

//        String message = "I have added work proposition!\n" +
//                "If you are interested, please visit page of this work.\n" +
//                "Description : \n" +
//                "Job type : " + work.getJobType().getTitle() + "\n" +
//                "About : " + work.getAbout() + "\n" +
//                "City : " + work.getCity().getRuName() + "\n" +
//                "Wage : " + work.getWage() + "\n" +
//                "<a href=\"/works/" + work.getIdWork() + "\">find out more</a>";


        for (SystemUser su : likers) {
            HashMap map = new HashMap();
            map.put("work", work);
            map.put("worker", su);
            String message = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "/newJob.vm", "UTF-8", map);
            messageService.sendMessage(employersEmail, su.getEmail(), "New work", message);
            mailService.sendMail(employersEmail, su.getEmail(), "New work", message);
        }

    }

    @Override
    @Async
    public void notifyLikersAndApplicantsAboutEditedWork(final Work work) {

        String employersEmail = work.getEmployer().getEmail();
        Set<SystemUser> likers = appDao.getLikesOfSystemUser(work.getEmployer().getIdUser(), 0);
        likers.addAll(work.getApplicants());

        for (SystemUser su : likers) {
            HashMap map = new HashMap();
            map.put("work", work);
            map.put("worker", su);
            String message = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "/editedJob.vm", "UTF-8", map);
            messageService.sendMessage(employersEmail, su.getEmail(), "Edited work", message);
            mailService.sendMail(employersEmail, su.getEmail(), "Edited work", message);
        }
    }

    @Override
    //todo russian message
    public void sendRecoveryInformation(String email, Long idRecovery) throws NoSuchEmailInSystemException {

        HashMap map = new HashMap();
        map.put("email", email);
        map.put("idRecovery", idRecovery);
        map.put("user", appDao.getSystemUserByEmail(email));
        String message = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "/recoveryInformation.vm", "UTF-8", map);
        mailService.sendMail("email@email.email", email, "Password recovery", message);
    }

    @Override
    public void sendLoginInformation(String email, Long idLogin) {
         HashMap map = new HashMap();
        map.put("email", email);
        map.put("idLogin", idLogin);
        map.put("user", appDao.getSystemUserByEmail(email));
        String message = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "/firstLogin.vm", "UTF-8", map);

        mailService.sendMail("noreply.maxicash@maxicash.com.ua", email, "Login information", message);
    }
}
